<template>
  <!-- 用了一个行列布局 -->
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartment } from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(type) {
      // console.log('触发了点击', type)
      if (type === 'add') {
        // console.log('添加操作')
        this.$emit('add-depts', this.nodeData)
      }

      if (type === 'del') {
        // console.log('删除操作')
        this.$confirm('此操作将永久删除该部门, 是否继续？', '温馨提示').then(async() => {
          // 确认
          await reqDelDepartment(this.nodeData.id)
          this.$message.success('删除成功！')
          // 调用方法，重新请求渲染(子传父，通知到父组件自己去更新)
          this.$emit('del-depts')
        }).catch(() => {
          console.log('取消')
        })
      }

      if (type === 'edit') {
        // console.log('编辑操作')
        this.$emit('edit-depts', this.nodeData)
      }
    }
  }
}
</script>

<style>

</style>
